package com.cloudcross.ssp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.OperatorAgentDetail;
import com.cloudcross.ssp.service.IOperatorAgentAccountService;

@Service
public class OperatorAgentService implements IOperatorAgentAccountService {

	@Autowired
	private GenericIBatisDao myBatisDao;
	
	@Override
	public OperatorAgentDetail findById(Long accountId) {
		return myBatisDao.get("operatorAgentAccountSqlMapper.findById", accountId);
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		String value = (String)paramMap.get("searchValue");
		if(value!=null){
			value = "%"+value+"%";
			paramMap.put("searchValue", value);
		}
		return myBatisDao.get("operatorAgentAccountSqlMapper.countByParams",paramMap);
	}

	@Override
	public List<OperatorAgentDetail> findByParams(Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		String value = (String)paramMap.get("searchValue");
		if(value!=null){
			value = "%"+value+"%";
			paramMap.put("searchValue", value);
		}
		return myBatisDao.getList("operatorAgentAccountSqlMapper.selectByParams",paramMap);
	}

	@Override
	public boolean add(OperatorAgentDetail t) {
		if (t.getStatus()==0){
			t.setStatus(1);
		}else{
			t.setStatus(0);
		}
		myBatisDao.save("operatorAgentAccountSqlMapper.addOperatorAgentAccount", t);
		return true;
	}
	
	public boolean addOperatorAgentDedail(OperatorAgentDetail t){
		myBatisDao.save("operatorAgentAccountSqlMapper.addOperatorAgentDedail", t);
		return true;
	}
	
	@Override
	public boolean edit(OperatorAgentDetail t) {
		if(t.getStatus()==0){
			t.setStatus(1);
		}else {
			t.setStatus(0);
		}
		myBatisDao.save("operatorAgentAccountSqlMapper.editOperatorAgentAccount", t);
		return true;
	}
	
	public boolean editOperatorAgentDetail(OperatorAgentDetail t){
		myBatisDao.save("operatorAgentAccountSqlMapper.editOperatorAgentDedail", t);
		return true;
	}
	
	@Override
	public boolean updateStatus(List<Long> idList, int status) {
		myBatisDao.save("operatorAgentAccountSqlMapper.updateAccountStatus", MapBuilder.create(HashMap.class)
				.add("accountIdList", idList)
				.add("status", status).map());
		return true;
	}

	@Override
	public boolean updateOperatorAgentStatus(List<Long> operatorAgentIdList, int status) {
		myBatisDao.save("operatorAgentAccountSqlMapper.updateOperatorAgentStatus", MapBuilder.create(HashMap.class)
				.add("operatorAgentIdList", operatorAgentIdList)
				.add("status", status).map());
		return true;
	}
	
	@Override
	public List<Long> findOperatorAgentId(List<Long> accountIds) {
		return myBatisDao.getList("operatorAgentAccountSqlMapper.findOperatorAgentId",MapBuilder.create(HashMap.class)
				.add("accountIdList", accountIds).map());
	}

	@Override
	public boolean deleteOperator(List<Long> accountIds) {
		myBatisDao.save("operatorAgentAccountSqlMapper.deleteAccount",
				MapBuilder.create(HashMap.class)
				.add("accountIdList",accountIds).map());
		return true;
	}
	
	public boolean hasOperatorAgent(long operatorId){
		int total = this.myBatisDao.get("operatorAgentSqlMapper.queryOperatorAgentByOperatorId", operatorId);
        return total != 0;
	}

	@Override
	public List<Long> findOperatorAgentZoneAuth(Long accountId) {
		return myBatisDao.getList("operatorAgentAccountSqlMapper.findOperatorAgentZoneAuth", accountId);
	}

}
